package com.example.tvrecyclerviewkotlin.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.tvrecyclerviewkotlin.R


/**
 *@author: njb
 *@date:  2021/2/1 11:51
 *@desc:
 */
class MyRecyclerViewAdapter(
    context: Context?
) : RecyclerView.Adapter<ViewHolder>() {
    private var mOnFocusChangeListener: OnItemFocusChangeListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null

    private var defaultFocus = 0
    private val needFocus = true
    private val mLayoutInflater: LayoutInflater = LayoutInflater.from(context)
    private var mStrings: Array<String>? = null

    fun setData(mStrings: Array<String>) {
        if (mStrings.isNullOrEmpty()) {
            return
        }
        this.mStrings = mStrings
        notifyDataSetChanged()
    }

    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
    }

    interface OnItemFocusChangeListener {
        fun onItemFocusChange(
            view: View?,
            position: Int,
            hasFocus: Boolean
        )
    }


    fun setOnItemClickListener(@Nullable listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }

    fun setOnFocusChangeListener(@Nullable mOnFocusChangeListener: OnItemFocusChangeListener?) {
        this.mOnFocusChangeListener = mOnFocusChangeListener
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val viewHolder = holder as RecyclerViewHolder
        viewHolder.tv.text = mStrings!![position]

        holder.itemView.setOnClickListener { v ->
            if (mOnItemClickListener != null) {
                mOnItemClickListener!!.onItemClick(v, position)
            }
        }

        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (mOnFocusChangeListener != null) {
                mOnFocusChangeListener!!.onItemFocusChange(v, position, hasFocus)
            }
        }

        holder.itemView.isFocusable = true
        if (defaultFocus < 0) {
            defaultFocus = 0
        }
        if (defaultFocus >= itemCount) {
            defaultFocus = itemCount - 1
        }
        if (defaultFocus == position) {
            if (!holder.itemView.isFocusable) {
                defaultFocus++
            } else {
                holder.itemView.requestFocus()
            }
        }
    }


    override fun getItemCount(): Int {
        return mStrings!!.size
    }

    private class RecyclerViewHolder internal constructor(itemView: View) : ViewHolder(itemView) {
        var tv: TextView = itemView.findViewById(R.id.tv)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v: View = mLayoutInflater.inflate(R.layout.layout_recyclerview_item, parent, false)
        return RecyclerViewHolder(v)
    }

}